# -*- coding: utf-8 -*-
"""
视频组件 

每个视频默认有 赤橙黄绿青蓝紫 7个图标，可以用来进行基本的分类

鼠标进入视频则自动播放，鼠标离开则停止播放
鼠标滚轮下滚，则向前加速
鼠标滚轮上滚，则向后倒退


"""
import sys 
from PySide2.QtMultimedia import *
from PySide2.QtMultimediaWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from ..utils.styles import STYLES
from .utilsWidgets import AddWidget

class ALVideoWidget(QWidget):

    def __init__(self,video_name,video_path):
        super(ALVideoWidget,self).__init__()

        self.flags = []
        # self.setFixedSize(QSize(200,200))
        self.main_layout = QVBoxLayout()
        self.flag_layout = QHBoxLayout()
        self.init_flags()
        self.progress = QLabel('')
        # self.progress.setFixedHeight(4)
        # self.progress.setFixedWidth(0)
        self.progress.setFixedSize(0,4)
        self.progress.setStyleSheet(STYLES.PLAYED_COLOR)
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.mediaPlayer.durationChanged.connect(self._duration_changed)
        self.mediaPlayer.positionChanged.connect(self._position_changed)
        self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(video_path)))
        self.videoWidget = QVideoWidget()
        self.mediaPlayer.setVideoOutput(self.videoWidget)
        self.videoWidget.setFixedSize(QSize(200,200))
 
        self.name_layout = QHBoxLayout()
        self.blank_label = QLabel('')

        self.blank_label.setFixedSize(QSize(20,20))
        self.name_label = QLabel(video_name)
        self.name_label.setFixedSize(QSize(150,20))
        self.name_label.setAlignment(Qt.AlignCenter)
        self.name_btn = QToolButton()
        self.name_btn.setStyleSheet(STYLES.TOOLBTN_FLAT)
        self.name_btn.setIcon(QIcon(QPixmap(STYLES.PEN_IMG_PATH)))
        self.name_btn.setIconSize(QSize(20, 20))
        self.name_btn.clicked.connect(self._rename)
        self.name_layout.addWidget(self.blank_label)
        self.name_layout.addWidget(self.name_label)
        self.name_layout.addWidget(self.name_btn)

        self.main_layout.addLayout(self.flag_layout)
        self.main_layout.addWidget(self.progress)
        self.main_layout.addWidget(self.videoWidget)
        self.main_layout.addLayout(self.name_layout)
        self.setLayout(self.main_layout)
        
        self.running = False
        self.num = 1*1000 # 这是毫秒
        # self.duration = self.mediaPlayer.duration()
       

    def init_flags(self):
        """
        初始化标签按钮
        """
        for color in STYLES.FLAG_COLORS:
            btn = QPushButton()
            
            btn.setProperty('color',color)
            btn.clicked.connect(self._set_flag)
            # btn.doubleClicked.connect(self._remove_flag)
            btn.setFixedSize(QSize(10,10))

            btn.setStyleSheet(STYLES.BTN_STYLE_B)
            self.flag_layout.addWidget(btn)

    def _set_flag(self):
     
        btn = self.sender()
        color = btn.property('color')
        print('颜色:',color)
        backrole = btn.backgroundRole()
        backcolor = backrole.Window.color()
        print('back-color:',backcolor)
        print(help(backrole.Background))
        btn.setStyleSheet('''QPushButton{color:black}
                        QPushButton:hover{color:#ebebeb}
                        QPushButton{background-color:%s}
                        QPushButton{border:2px}
                        QPushButton{border-radius:5px}
                        QPushButton{padding:1px 1px}'''  %color)
        

    def _duration_changed(self,duration:int):
        """
        槽函数，当播放时间改变时
        """
        print('duration:',duration)
        self.duration = duration

    def _position_changed(self,position:int):
        """
        当播放位置更改时
        """
        print('position:',position)
        show_length = position*(200.0/self.duration)
        print('show_length:',show_length)
        self.progress.setFixedWidth(show_length)

    def _rename(self):
        """
        重命名
        """
        self.name_widget = AddWidget('命名','名称:')
        self.name_widget.add_Signal.connect(self._set_name)
        self.name_widget.show()

    def _set_name(self,name):
        # 数据库中重命名
        self.name_label.setText(name)


    # def _remove_flag(self):
    #     """
    #     移除该标识
    #     """
    #     btn = self.sender()
    #     btn.setStyleSheet(STYLES.FLAG_BTN_BLANK)

    # def underMouse(self):
    #     print('放我头上了')
    #     return super().underMouse()

    # def mouseMoveEvent(self, event):
    #     print('放我头上了')
    #     return super().mouseMoveEvent(event)


    def enterEvent(self,event):
        print('鼠标进来了')
        print('position:',self.mediaPlayer.position())
        if not self.running:
            self.mediaPlayer.play()
        self.running = True
        return super().enterEvent(event)

    def leaveEvent(self,event) -> None:
        print('鼠标出去了')
        if self.running:
            self.mediaPlayer.pause()
        self.running = False
        return super().leaveEvent(event)


    def wheelEvent(self, event):
        if event.type() == QEvent.Wheel:
            delta = event.delta()
            hv = self.mediaPlayer.position()
            if delta>0: # 网上滚
                hv -= self.num
            else:
                hv += self.num
            self.mediaPlayer.setPosition(hv)
        return super().wheelEvent(event) 

